package com.rumenz.controller.mapper;



import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rumenz.controller.User;
import com.rumenz.controller.service.UserMapper;
import com.rumenz.controller.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: UserServiceimpl
 * @description: TODO 类描述
 * @author: 入门小站 rumenz.com
 * @date: 2021/12/28
 **/

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer save(String name, String domian, Integer age) {
        User user=User.builder().name(name).domain(domian).age(age).build();
        Integer retint = userMapper.insert(user);
        return retint;
    }

    @Override
    public List<User> list() {

        List<User> list = userMapper.selectList(null);
        return list;
    }

    @Override
    public Integer update(Integer id, String name, String domain, Integer age) {
        User user=new User();
        UpdateWrapper<User> updateWrapper=new UpdateWrapper<User>();
        updateWrapper.set("name", name);
        updateWrapper.set("domain", domain);
        updateWrapper.set("age", age);
        //where 条件
        updateWrapper.eq("id", id);
        Integer retint = userMapper.update(user,updateWrapper);
        return retint;
    }

    @Override
    public Integer delete(Integer id) {

        return  userMapper.deleteById(id);
    }

    @Override
    public Integer saveList() {


        List<User> list=new ArrayList<>();
        User user1 = User.builder().name("批量1").domain("https://rumenz.com").age(1).build();
        User user2 = User.builder().name("批量2").domain("https://rumenz.com").age(2).build();
        User user3 = User.builder().name("批量3").domain("https://rumenz.com").age(3).build();
        User user4 = User.builder().name("批量4").domain("https://rumenz.com").age(4).build();
        User user5 = User.builder().name("批量5").domain("https://rumenz.com").age(5).build();
        User user6 = User.builder().name("批量6").domain("https://rumenz.com").age(6).build();

        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        list.add(user6);
        this.saveBatch(list);
        return 0;
    }

    @Override
    public User getByById(Integer id) {

        return  userMapper.getById(id);
    }
}
